package logic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"group-card/common/constant"
	"group-card/common/request"
	"group-card/model/entity"
	"group-card/rpc/card/cardclient"
	"group-card/rpc/card/internal/svc"
)

type BindingListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBindingListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BindingListLogic {
	return &BindingListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *BindingListLogic) BindingList(in *cardclient.BindingListRequest) (*cardclient.BindingListResponse, error) {
	var ctx context.Context
	req := &entity.CardBindingReq{
		UserId:     in.UserId,
		CardNumber: in.CardNumber,
		FaceValue:  in.FaceValue,
		Status:     in.Status,
		BaseReq: request.BaseReq{
			PageNum:  in.PageNum,
			PageSize: in.PageSize,
		},
	}
	data, count, err := l.svcCtx.BindingModel.List(ctx, req)
	if err != nil {
		return nil, err
	}

	var list []*cardclient.BindingListData
	for _, value := range data {
		list = append(list, &cardclient.BindingListData{
			Id:            value.Id,
			CardNumber:    value.CardNumber,
			CustomerId:    value.CustomerId,
			UserId:        value.UserId,
			BindingTime:   value.BindingTime.Format(constant.YMDHIS),
			FaceValue:     value.FaceValue,
			Balance:       value.Balance,
			Status:        value.Status,
			ServiceNumber: value.ServiceNumber,
			CreateAt:      value.CreateAt.Format(constant.YMDHIS),
			UpdateAt:      value.UpdateAt.Format(constant.YMDHIS),
			DeleteAt:      value.DeleteAt.Format(constant.YMDHIS),
		})
	}

	return &cardclient.BindingListResponse{
		Total: count,
		List:  list,
	}, nil

}
